// Copyright 2009 StarCite Inc.  All rights reserved.
// TrainService.java 4:51:22 PM danny.liu
package com.billowsoft.tool.train.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.message.BasicHeader;

/**
 * @author danny.liu Feb 1, 2012
 * @version 1.0
 * 
 */
public class TrainService {

	public static void queryTrain(String fromCity, String toCity, Date date)
			throws Exception {

		Map<String, String> queryParams = new HashMap<String, String>();

		queryParams.put("orderRequest.from_station_name", fromCity);
		queryParams.put("orderRequest.to_station_name", toCity);
		queryParams.put("orderRequest.train_date", convertDate(date));
		queryParams.put("orderRequest.start_time_str", "00:00--24:00");
		queryParams.put("org.apache.struts.taglib.html.TOKEN",
				"ee0b8da041a85e9519c04e2940f46e6f");
		queryParams.put("singleRoundType", "1");
		queryParams.put("ccType", "QB");
		queryParams.put("trainPassType", "QB");

		HttpUtil.sendFormBasedRequest(HttpClientFactory.getHttpClient(),
				Configuration.QUERY_TRAIN_URL, queryParams);
	}

	public static void orderTickets(String trainNumber, Date date,
			List<String> passengerIdNumbers, String passengerPhoneNumber,
			String randCode) throws Exception {

		Map<String, String> orderParams = new HashMap<String, String>();

		// orderParams.put("orderRequest.train_date", convertDate(date));
		orderParams.put("checkbox0", "Y");
		orderParams.put("checkbox9", "Y");
		orderParams.put("randCode", randCode);
		orderParams.put("orderRequest.train_date", "2012-02-23");
		orderParams.put("orderRequest.train_no", "550000K13700");
		orderParams.put("orderRequest.station_train_code", "K137");
		orderParams.put("orderRequest.from_station_telecode", "SNH");
		orderParams.put("orderRequest.to_station_telecode", "CSQ");
		orderParams.put("orderRequest.seat_type_code", "");
		orderParams.put("orderRequest.ticket_type_order_num", "");
		orderParams.put("orderRequest.bed_level_order_num",
				"000000000000000000000000000000");
		orderParams.put("orderRequest.start_time", "16:55");
		orderParams.put("orderRequest.end_time", "07:06");
		orderParams.put("orderRequest.from_station_name", "上海南");
		orderParams.put("orderRequest.to_station_name", "长沙");
		orderParams.put("orderRequest.cancel_flag", "1");
		orderParams.put("orderRequest.id_mode", "Y");
		orderParams.put("orderRequest.reserve_flag", "A");
		orderParams.put("oldPassengers", "刘波,1,430181198503217819");
		orderParams.put("org.apache.struts.taglib.html.TOKEN",
				"b60c13132fe1fb0854dca4ef6f9604c0");
		orderParams.put("passengerTickets",
				"3,1,刘波,1,430181198503217819,13764487451,Y");

		int index = 1;

		for (String idNumber : passengerIdNumbers) {

			orderParams.put(String.format("passenger_%d_name", index), "刘波");
			orderParams.put(String.format("passenger_%d_seat", index), "3");
			orderParams.put(String.format("passenger_%d_ticket", index), "1");
			orderParams.put(String.format("passenger_%d_cardType", index), "1");
			orderParams.put(String.format("passenger_%d_cardno", index),
					"430181198503217819");
			orderParams.put(String.format("passenger_%d_mobileno", index),
					"13764487451");

			index++;
		}

		HttpUtil
				.sendFormBasedRequest(
						HttpClientFactory.getHttpClient(),
						Configuration.ORDER_TICKET_URL,
						orderParams,
						new BasicHeader("Referer",
								"https://dynamic.12306.cn/otsweb/order/confirmPassengerAction.do?method=init"),
						new BasicHeader("Origin", "https://dynamic.12306.cn"));
	}

	private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

	private static String convertDate(Date date) {
		return format.format(date);
	}
}
